using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Xml;
using System.Xml.Serialization;
using Google.GData.Client;

namespace ScottLane.VideoInbox
{
    /// <summary>
    /// A video source that downloads video podcasts from the web using xml feeds.
    /// </summary>
    public class VideoPodcastSource : DownloadableVideoSource
    {
        /// <summary>
        /// Initializes the VideoPodcastSource class.
        /// </summary>
        public VideoPodcastSource()
        {
            _feeds = new List<string>();
            _feeds.Add("http://scottelane.wordpress.com/wp-links-opml.php?link_cat=7685");
            _feeds.Add("http://feeds.feedburner.com/sc2ggpodcast?format=xml");
        }

        private List<string> _feeds;

        /// <summary>
        /// Gets or sets to opml, atom or rss feeds to download.
        /// </summary>
        public List<string> Feeds
        {
            get { return _feeds; }
            set { _feeds = value; }
        }

        public override void FindNewVideos()
        {
            foreach (string feed in _feeds)
            {
                GetFeedVideos(feed);
            }
        }

        private void GetFeedVideos(string feed)
        {
            using (XmlReader reader = XmlTextReader.Create(feed))
            {
                while (reader.NodeType != XmlNodeType.Element)
                {
                    reader.Read();
                }

                if (reader.Name == "opml")
                {
                    reader.ReadToFollowing("outline");

                    if (reader.ReadToDescendant("outline"))
                    {
                        reader.MoveToAttribute("xmlUrl");
                        GetFeedVideos(reader.Value);
                    }                    
                }
                else if (reader.Name == "rss")
                {
                    //XmlSerializer serializer = new XmlSerializer(typeof(RssFeed));
                    //RssFeed rss = (RssFeed)serializer.Deserialize(new XmlTextReader(feed));

                    //reader.ReadToDescendant("title");
                    //string title = reader.Value;

                    //while (reader.ReadToFollowing("item"))
                    //{
                    //    Video video = new Video();

                    //    if (reader.ReadToFollowing("title"))
                    //    {
                    //        video.Title = reader.Value;
                    //    }

                    //    XmlSerializer serializer = new XmlSerializer(null);
                    //    serializer.Deserialize(

                    //}
                }
            }
        }
    }
}
